<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-02-24
 * Time: 12:15
 */

namespace app\service;

use app\model\Area;

class AreaService extends BaseService{


    public $key = '';

    function children($id = 0){
        $url = "https://apis.map.qq.com/ws/district/v1/getchildren?key=".$this->key;

        if($id) $url .= "&id=".$id;

        $result = curl($url);
        $result = json_decode($result, true);

        if($result['status'] == 0){
            return $result['result'];
        }else{
            return false;
        }
    }

    function updateData($data = [], $parent_id = 0, $level = 1){

        $area_model = new Area();

        foreach ($data as $vv){
            foreach ($vv as $v){
                $area_model->addData([

                    'name'=> $v['name'],
                    'fullname'=> $v['fullname'],
                    'pinyin'=> implode(' ', $v['pinyin']),
                    'lat'=> $v['location']['lat'],
                    'lng'=> $v['location']['lng'],
                    'parent_code'=> $parent_id,
                    'code'=> $v['id'],
                    'level'=> $level
                ]);
            }
        }

    }


}